package br.com.salao.ejb.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.ServicosDao;
import br.com.salao.entity.Servicos;

public class ServicosDaoImpl implements ServicosDao {

	private final GenericDao<Servicos> dao;
	private Logger logger = Logger.getLogger(ServicosDaoImpl.class.getName());
	private EntityManager em;

	public ServicosDaoImpl(EntityManager em) {
		this.em = em;
		dao = new GenericDao<Servicos>(em, Servicos.class);
	}

	public Servicos inserir(Servicos servicos) {		
		return dao.salvar(servicos);
	}

	public List<Servicos> listar(){		
		return dao.lista();
	}		

	public Servicos update(Servicos servicos){
		return dao.update(servicos);
	}

	@SuppressWarnings("unchecked")
	public List<Servicos> buscarServicoPorIdEstabelecimento(int idEstabelecimento){

		List<Servicos> servicos = null;

		try {		
			Query query = em.createQuery("from Servicos s where s.estabelecimento.id like :idEstabelecimento");
			query.setParameter("idEstabelecimento", idEstabelecimento);
			servicos = (List<Servicos>) query.getResultList();	

		} catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getMessage() + "\n");
			sb.append(e.getCause());
			logger.error(sb.toString());

			e.printStackTrace();
			servicos = null;

		} 
		return servicos;			
	}	

	public Servicos buscarServicoPorId(int id){

		Servicos servico = null;

		try {		
			Query query = em.createQuery("from Servicos s where s.id like :id");
			query.setParameter("id", id);
			servico = (Servicos) query.getSingleResult();	

		} catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getMessage() + "\n");
			sb.append(e.getCause());
			logger.error(sb.toString());

			e.printStackTrace();
			servico = null;

		} 
		return servico;			
	}
}
